package com.qingxin.inspection.action;

import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import com.qingxin.cms.entity.InspectionTask;
import com.qingxin.inspection.service.InspectionTaskService;
import com.qingxin.sys.action.BaseAction;
import com.qingxin.sys.mvc.PageParam;
import com.qingxin.sys.mvc.SysError;
import com.qingxin.sys.support.JsTreeNode;
import com.qingxin.sys.utils.StringUtils;

/**
 * 巡查管理
 * @author Administrator
 *
 */
@Controller
@RequestMapping("/inspection/task")
public class InspectionTaskAction extends BaseAction {
	
	@Autowired
	private InspectionTaskService inspectionTaskService;
	
	
	@RequestMapping(value="/list.do", method=RequestMethod.GET)
	public String list(ModelMap model, HttpServletRequest request){
		return "trace/inspection/task/task_list";
	}
	
	/**
	 * 加载左侧 类别-文章
	 * @param pid
	 * @return
	 */
	@RequestMapping(value="/lefttree.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public List<JsTreeNode> lefttree(String pid, HttpServletRequest request){
		List<JsTreeNode> list = new ArrayList<JsTreeNode>();
		if(StringUtils.isEmpty(pid)){
			JsTreeNode root = new JsTreeNode("0", "所有任务","inspect_TASK", null, true);
			list.add(root);
		}else{
			list.addAll(inspectionTaskService.getUITreeData());
		}
		return list;
	}
	
	/**
	 * 获取列表数据
	 * @param parkName
	 * @param parkStatus
	 * @param pageParam
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/listdata.json", method={RequestMethod.GET, RequestMethod.POST})
	@ResponseBody
	public Object listdata(PageParam pageParam,String search,HttpServletRequest request){
		return inspectionTaskService.list(pageParam,search,request);
	}
	

	/**
	 * 删除巡查任务
	 * @param taskid
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/delete.json", method=RequestMethod.POST)
	@ResponseBody
	public Object delete(String[] taskId, HttpServletRequest request){
		return inspectionTaskService.delete(taskId, request);
	}
	/**
	 * 添加巡查任务
	 * @param model
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/add.do", method=RequestMethod.GET)
	public String add(ModelMap model, HttpServletRequest request){
		return "/trace/inspection/task/task_add";
	}
	
	/**
	 * 添加保存
	 * @param task
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/save.json", method={RequestMethod.POST})
	@ResponseBody
	public Object save(@Valid InspectionTask task, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		
		SysError info = inspectionTaskService.add(task, getLoginUser(request), request);
		return info;
	}
	
	/**
	 * 修改巡查任务
	 * @param model
	 * @param taskId
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/edit.do", method=RequestMethod.GET)
	public String edit(ModelMap model, String taskId, HttpServletRequest request){
		InspectionTask task = inspectionTaskService.get(taskId);
		model.put("task", task);
		return "trace/inspection/task/task_edit";
	}
	
	/**
	 * 修改保存
	 * @param task
	 * @param result
	 * @param request
	 * @return
	 */
	@RequestMapping(value="/update.json", method=RequestMethod.POST)
	@ResponseBody
	public Object update(@Valid InspectionTask task, BindingResult result, HttpServletRequest request){
		if(result.hasErrors()){
			return getSysErrors(result);
		}
		return inspectionTaskService.update(task, getLoginUser(request), request);
	}
	
	/**
	 * 判断任务名是否存在
	 * @param inspecName
	 * @param taskId
	 * @return
	 */
	@RequestMapping(value="/isNameDuplicate.json" , method=RequestMethod.GET)
	@ResponseBody
	public String isNameDuplicate(String inspecName, String taskId){
		boolean existed = inspectionTaskService.isNameDuplicate(inspecName, taskId);
		return existed ? "1":"0";
	}
	
	
	/**
	 * 判断任务编码是否存在
	 * @param inspecCode
	 * @param taskId
	 * @return
	 */
	@RequestMapping(value="/isNoDuplicate.json" , method=RequestMethod.GET)
	@ResponseBody
	public String isNoDuplicate(String inspecCode, String taskId){
		boolean existed = inspectionTaskService.isNoDuplicate(inspecCode, taskId);
		return existed ? "1":"0";
	}

}
